class Solution {
public:
    string reverseWords(string s) {
        auto it = s.begin();
            for (auto i = s.begin(); i != s.end(); i++) {
                if (*i == ' ') {
                    if (it < i - 1) {
                        reverse(it, i);
                    }
                    it = i + 1;
                }
            }
            if (it != s.end()) {
                reverse(it, s.end());
            }
            return s;
        }
};